<?php

class Application_Model_App extends Zend_Db_Table_Abstract
{

    protected $_name = "kp_apps"; //Ten bang
    protected $_primary = "app_id"; //Khoa chinh
    protected $db;
    //Cac truong du lieu
    var $app_id;
    var $user_id;
    var $dev_id;
    var $app_name;
    var $app_icon;
    var $app_des_short;
    var $app_description;
    var $app_video;
    var $app_type_id;
    var $cat_id;
    var $app_website;
    var $app_email;
    var $app_phone;
    var $app_date_create;
    var $app_date_publish;
    var $app_status=0; //0:chua hoan thanh; 1:da hoan thanh cho xac nhan; 2: da xac nhan
    
    
    //Ham khoi tao
    public function __construct(){
        $this->db = Zend_Registry::get('db');
    }
    
    //Get all user
    public function getAll($status=-1){
        $query = $this->db->select();
        $query->from($this->_name);
        if($status!=-1)$query->where('cat_status = ?',$status);
        $query->order(array('cat_name'));
        return $this->db->fetchAll($query);
    }
    
    //Them mới user
    public function insertNew(){
        $data = array(
                'app_id' => $this->app_id,
                'user_id' => $this->user_id,
                'app_name' => $this->app_name,
                'app_icon' => $this->app_icon,
                'app_des_short'=>$this->app_des_short,
                'app_description'=>$this->app_description,
                'app_video'=>$this->app_video,
                'app_website'=>$this->app_website,
                'app_email'=>$this->app_email,
                'app_phone'=>$this->app_phone,
                'app_status'=>$this->app_status,
                'cat_id'=>$this->cat_id,
                'app_type_id' => $this->app_type_id
        );
        //var_dump($data); die;
        return $this->db->insert($this->_name,$data);
    }
    public function insertAdd1(){
        $data = array(
                'app_id' => $this->app_id,
                'user_id' => $this->user_id,
                'dev_id' => $this->dev_id,
                'app_name' => $this->app_name,
        );
        //var_dump($data); die;
        return $this->db->insert($this->_name,$data);
    }
    public function insertAdd2(){
        $data = array(
                'cat_id' => $this->cat_id,
        );
        $where = array('app_id = ?'=>$this->app_id);
        //var_dump($data); die;
        return $this->db->update($this->_name,$data,$where);
    }
    public function updateData($data){
        
        $where = array('app_id = ?'=>$this->app_id);
        //var_dump($data); die;
        return $this->db->update($this->_name,$data,$where);
    }
     public function getByName($name){
         $query = $this->db->select();
         $query->from($this->_name);
         $query->where('app_name = ?',$name);
         return $this->db->fetchAll($query);
     }
     public function getByID($id){
         $query = $this->db->select();
         $query->from($this->_name.' as app');
         $query->join('kp_categories as cat','app.cat_id = cat.cat_id');
         $query->join('kp_dev_info as dev','dev.dev_id = app.dev_id');
         $query->where('app_id = ?',$id);
         return $this->db->fetchRow($query);
     }
     
     public function getTopNew(){
         $query = $this->db->select();
         $query->from($this->_name.' as app');
         $query->join('kp_dev_info as dev','dev.dev_id = app.dev_id');
         //$query->where('app_status = ?',0);
         $query->order(array('app_date_create DESC'));
         return $this->db->fetchAll($query);
     }
     
     public function getTopNewIndex(){
         $query = $this->db->select();
         $query->from($this->_name.' as app');
         $query->join('kp_dev_info as dev','dev.dev_id = app.dev_id');
         $query->where('app_status = ?',2);
         $query->order(array('app_date_publish DESC'));
         return $this->db->fetchAll($query);
     }
     
}

